package com.cgtong.android.mvp.presenter;

import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.mvp.contract.RecycleListContract;
import com.cgtong.android.mvp.model.RecycleListModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import java.util.List;

/**
 * Created by chunc on 2018/1/3.
 */

public class RecycleListPresenter implements RecycleListContract.Presenter {

    private RecycleListContract.Model mModel;
    private RecycleListContract.View mView;

    public RecycleListPresenter(RecycleListContract.View view) {
        this.mView = view;
        mModel = new RecycleListModel();
    }

    @Override
    public void httpRecycleList(long maxId, String searchKey, long userId, final boolean isRefresh) {
        if (isRefresh) {
            mView.setEnableLoadMore(false);
            mView.setRefreshing(true);
        }
        mModel.httpRecycleList(maxId, searchKey, userId, new BaseCallBack<List<ProblemBean>>() {
            @Override
            public void onSuccess(List<ProblemBean> problemBeen) {
                if (mView != null) {
                    if (isRefresh) {
                        mView.setRefreshing(false);
                        mView.setEnableLoadMore(true);
                        if (problemBeen == null || problemBeen.size() == 0) {
                            mView.onNullData();
                            return;
                        } else if (problemBeen.size() < ConstantManager.MY_REPORTS_PAGE_SIZE) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                        mView.onHttpRecycleListSuccess(problemBeen, isRefresh);
                    } else {
                        mView.onHttpRecycleListSuccess(problemBeen, isRefresh);
                        if (problemBeen.size() < ConstantManager.MY_REPORTS_PAGE_SIZE) {
                            mView.onLoadMoreEnd(isRefresh);
                        }
                    }
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null) {
                    mView.setRefreshing(false);
                    mView.onError(e);
                }
            }
        });
    }

    @Override
    public void commitRecycleProblemIDs(String ids) {
        mModel.commitRecycleProblemIDs(ids, new BaseCallBack<ApiResult>() {
            @Override
            public void onSuccess(ApiResult apiResult) {
                if (mView != null) {
                    mView.onCommitRecycleProblemIDsSuccess(apiResult);
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null)
                    mView.onError(e);
            }

        });
    }

    @Override
    public void deleteRecycles(String ids) {
        mModel.deleteRecycles(ids, new BaseCallBack<ApiResult>() {
            @Override
            public void onSuccess(ApiResult apiResult) {
                if (mView != null) {
                    mView.onDeleteRecycleIDsSuccess(apiResult);
                }
            }

            @Override
            public void onError(ApiException e) {
                if (mView != null)
                    mView.onError(e);
            }

        });
    }

    @Override
    public void onDestroy() {
        if (mView != null) {
            mView = null;
        }
    }
}
